'use strict';

const xhr = new XMLHttpRequest();
let imgs = document.querySelectorAll('img');

window.addEventListener('load', () => {
    let promise = new Promise((resolve, reject) => {
        xhr.open('get', '/get_pic?classN=view', true);
        xhr.send();
        xhr.onreadystatechange = () => {
            if (xhr.readyState === 4 && xhr.status === 200) {
                if (xhr.responseText === 'no') {
                    alert('获取失败！');
                    return;
                }
                if (JSON.parse(xhr.responseText).infoArr[0]) {
                    imgs[1].setAttribute('data-class', JSON.parse(xhr.responseText).infoArr[0].class);
                    imgs[1].setAttribute('data-src', JSON.parse(xhr.responseText).infoArr[0].name);
                }
                resolve();
            }
        }
    })
    promise.then((value) => {
        return new Promise((resolve, reject) => {
            xhr.open('get', '/get_pic?classN=manga', true);
            xhr.send();
            xhr.onreadystatechange = () => {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    if (xhr.responseText === 'no') {
                        alert('获取失败！');
                        return;
                    }
                    if (JSON.parse(xhr.responseText).infoArr[0]) {
                        imgs[2].setAttribute('data-class', JSON.parse(xhr.responseText).infoArr[0].class);
                        imgs[2].setAttribute('data-src', JSON.parse(xhr.responseText).infoArr[0].name);
                    }
                    resolve();
                }
            }
        })
    }).then((value) => {
        return new Promise((resolve, reject) => {
            xhr.open('get', '/get_pic?classN=cate', true);
            xhr.send();
            xhr.onreadystatechange = () => {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    if (xhr.responseText === 'no') {
                        alert('获取失败！');
                        return;
                    }
                    if (JSON.parse(xhr.responseText).infoArr[0]) {
                        imgs[3].setAttribute('data-class', JSON.parse(xhr.responseText).infoArr[0].class);
                        imgs[3].setAttribute('data-src', JSON.parse(xhr.responseText).infoArr[0].name);
                    }
                    resolve();
                }
            }
        })
    }).then((value) => {
        return new Promise((resolve, reject) => {
            xhr.open('get', '/get_pic?classN=life', true);
            xhr.send();
            xhr.onreadystatechange = () => {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    if (xhr.responseText === 'no') {
                        alert('获取失败！');
                        return;
                    }
                    if (JSON.parse(xhr.responseText).infoArr[0]) {
                        imgs[4].setAttribute('data-class', JSON.parse(xhr.responseText).infoArr[0].class);
                        imgs[4].setAttribute('data-src', JSON.parse(xhr.responseText).infoArr[0].name);
                    }
                    resolve();
                }
            }
        })
    }).then((value) => {
        setTimeout(() => {
            const xhr = new XMLHttpRequest();
            for (let i = 1; i < 5; i++) {
                if (imgs[i].getAttribute('data-src')) {
                    let classN = imgs[i].getAttribute('data-class');
                    let name = imgs[i].getAttribute('data-src');
                    xhr.open('get', `/get_pic_file?classN=${classN}&name=${name}`, false);
                    xhr.send();
                    if (xhr.readyState === 4 && xhr.status === 200) {
                        if (xhr.responseText === 'no') {
                            alert('图片加载失败！');
                            return;
                        }
                        imgs[i].src = xhr.responseText;
                    }
                }
            }
        }, 100)
    })
})


//相册图片缩放效果
class imgScaleAni {
    constructor() {
        this.getEle();
        this.imagesScale();
    }

    getEle() {
        this.imagesWrap = document.querySelector('.images_wrap');
        this.as = this.imagesWrap.querySelectorAll('a');
    }

    imagesScale() {
        for (let i = 0; i < this.as.length; i++) {
            this.as[i].addEventListener('mouseenter', () => {
                this.as[i].className = 'img_scale_enter';
            })
            this.as[i].addEventListener('mouseleave', () => {
                this.as[i].className = 'img_scale_leave';
            })
        }
    }
}

new imgScaleAni();